import Call, { Api } from "./call"

/** 配置项 */
export interface ConfigJSON {
    /** 基础路径 */
    BaseUrl: string,

    /** IO.API 基础地址 */
    IOFileApi: {
        BaseUrl: string,
    }
}

let rConfigJSON: ConfigJSON = null;

/**
 * 获取全局配置
 * @returns 获取配置
 */
export default async function GetConfigJSON(): Promise<ConfigJSON> {
    if (rConfigJSON === null) {
        rConfigJSON = await RequestConfigJSON();
    }
    return rConfigJSON;
}

/**
 * 请求获取配置JSON内容
 * @returns 配置项结果
 */
async function RequestConfigJSON(): Promise<ConfigJSON> {
    let response = await Call(new Api("/configs/release.json", "get"));
    if (response.IsError) {
        console.error("配置查询出错:");
        console.error(`错误消息: ${response.ErrorMessage || ''}`);
        console.error("异常:", response.Excption);
        return null;
    }
    let releaseJSON = response.Data;
    if (!releaseJSON) {
        console.error("配置查询结果为空:");
        console.error(`配置查询消息: ${response.ErrorMessage || ''}`);
        console.error(`配置查询结果: ${releaseJSON}`);
        return null;
    }
    console.log("releaseJSON:", releaseJSON);
    return releaseJSON as ConfigJSON;
}
